import { defineStore } from 'pinia'
import { UserInfo } from '../ts/types/User'
import { UserState } from '../ts/types/UserState'
import { ApiService } from '../ts/ApiService'
import { BaseDataResult } from '../ts/types/BaseResult'
import { Menu } from '../ts/types/Common'

const store = defineStore('main', {
  state: (): UserState => {
    return {
      /**
       * 是否已经登录
       */
      islogin: false,
      /**
       * 登录用户信息
       */
      userinfo: {} as UserInfo,
    }
  },
  getters: {
    genders: () => {
      return [
        { text: '男', value: 'm' },
        { text: '女', value: 'f' },
        { text: '保密', value: 'n' },
      ]
    },
    usermenu: () => {
      return [
        {
          link: '/user/note',
          title: '记事本',
          icon: '/svg/usernote.svg',
        },
        {
          link: '/file/manage',
          title: '文件管理',
          icon: '/svg/filemanage.svg',
        },
        {
          link: '/file/ossconfig',
          title: 'oss-配置管理',
          icon: '/svg/ossconfig.svg',
        },
        {
          link: '/file/ossbucket',
          title: 'oss-bucket管理',
          icon: '/svg/ossbucket.svg',
        },
        {
          link: '/file/ossinfo',
          title: 'oss-文件管理',
          icon: '/svg/ossinfo.svg',
        },
        {
          link: '/game/idiom',
          title: '成语小游戏',
          icon: '/svg/idiom.svg',
        },
        {
          link: '/game/snake',
          title: '贪吃蛇',
          icon: '/svg/snake.svg',
        },
      ] as Array<Menu>
    },
    mainmenu: () => {
      return [
        {
          link: '/basic/link',
          title: '数据联动',
          icon: '/svg/link.svg',
        },
        {
          link: '/basic/crud',
          title: '增删改查',
          icon: '/svg/crud.svg',
        },
        {
          link: '/basic/andcrud',
          title: '联表增删改查',
          icon: '/svg/andcrud.svg',
        },
        {
          link: '/charts/user',
          title: '用户统计图表',
          icon: '/svg/userchart.svg',
        },
        {
          link: '/charts/message',
          title: '发帖统计图表',
          icon: '/svg/messagechart.svg',
        },
      ] as Array<Menu>
    },
    moremenu: () => {
      return [
        {
          link: '/user/login',
          title: '登录查看更多',
          icon: '/svg/more.svg',
        },
      ] as Array<Menu>
    },
  },
  actions: {
    /**
     * 更新登录用户信息
     * @param info  登录用户信息
     */
    updateUserInfo(islogin: boolean, userinfo: UserInfo) {
      this.islogin = islogin
      this.userinfo = userinfo
    },
    /**
     *
     * 查询服务器登录用户信息
     * @param cb 查询完毕之后的回调，回调的参数为是否登录
     */
    queryUserInfo(cb?: Function) {
      const instance = this
      ApiService.get('/auth', {}, (data: BaseDataResult<UserInfo>) => {
        instance.updateUserInfo(data.success, data.data)
        if (cb) {
          cb(data.success)
        }
      })
    },
  },
})

export default store
